package com.calefk.modules.ui {

    import fl.controls.ScrollBar;
    import fl.events.ScrollEvent;
    import flash.display.MovieClip;
    import flash.text.TextField;

    public class UIScrollText {
        
        private var mTextfield:TextField;
        private var mScroll:ScrollBar;
        private var mPositionBar:Number;
        
        private var mHText:Number;
        private var mOriginY:Number;
        private var mOriginH:Number;
        
        private var mBlack:MovieClip;
        
        
        public function UIScrollText(textfield:TextField , scrollbar:ScrollBar , direction:String = "vertical") {
            mTextfield = textfield;
            mScroll = scrollbar;
            
            // get poitn origin
            mOriginY = mTextfield.y;
            mOriginH = mTextfield.height;
            
            // fix height
            mHText = mTextfield.textHeight + 5;
            mTextfield.height = mHText;
            
            // create mask
            mBlack = new MovieClip();
            mBlack.graphics.beginFill(0x000000 , 1);
            mBlack.graphics.drawRect(0 , 0 , mTextfield.width , mOriginH);
            mBlack.graphics.endFill();
            mBlack.x = mTextfield.x;
            mBlack.y = mTextfield.y;
            // add to canvas
            mTextfield.parent.addChild(mBlack);
            mTextfield.mask = mBlack;
            
            // activar propiedades
			mScroll.enabled = true; 
			mScroll.direction = direction;
			mScroll.setScrollProperties(mOriginH, 0 , (mHText - mOriginH));
			mScroll.addEventListener(ScrollEvent.SCROLL, onScrollPress , false , 0 , true);
            
            // recovery position
            mTextfield.y = mOriginY;
            mPositionBar = mOriginY;
        }
        
        private function onScrollPress(e:ScrollEvent):void {
           mPositionBar = -e.position + mOriginY;
        }
        
        public function update(dt:int):void {
            // effect easing
            var vy:Number = (mPositionBar - mTextfield.y) * 0.2;
            mTextfield.y += vy;
        }
        
        public function clean():void {
            mTextfield.parent.removeChild(mBlack); 
            mBlack = null;
            mTextfield = null;
            mScroll = null;
        }
        
    }

}
